package com.xframe.common.core.service.register;


import com.xframe.common.core.service.IProcCommandService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Log4j2
//@Service("register")
public class CommandRegisterService implements InitializingBean, ApplicationContextAware {

    private Map<String, IProcCommandService> serviceImplMap = new HashMap<>();

    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {


        Map<String, IProcCommandService> beanMap = applicationContext.getBeansOfType(IProcCommandService.class);

        for (IProcCommandService serviceImpl : beanMap.values()) {
            serviceImplMap.put(serviceImpl.getBeanName(), serviceImpl);
            log.info("Command === {}",serviceImpl.getClass());
        }
        log.info("CommandRegisterService.afterPropertiesSet,命令总数==== {}",serviceImplMap.size());

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("CommandRegisterService.setApplicationContext ====");
        this.applicationContext = applicationContext;
    }

    public IProcCommandService getServiceImpl(String name) {
        return serviceImplMap.get(name);
    }

    public IProcCommandService getServiceImpl() {
        return serviceImplMap.get("defCommand");
    }
}
